package com.heima.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleContentService;
import com.heima.article.service.ApArticleHtmlService;
import com.heima.article.service.ApArticleService;
import com.heima.file.service.FileStorageService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: wxw
 * @ClassName: ApArticleHtmlServiceImpl
 * @JdkVersion: JDK1.8
 * @Version: 1.0.0
 * @Since: 2025/2/19 19:58
 * @Description:
 */
@Service
@Slf4j
public class ApArticleHtmlServiceImpl implements ApArticleHtmlService {

    @Autowired
    private Configuration configuration;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private ApArticleService apArticleService;



    /**
     * 异步生成文章详情页并上传到MinIO
     */
    @GlobalTransactional
    @Async("taskExecutor")
    @Override
    public void generatorHtml(ApArticle apArticle, String contentStr) {

        try {
            //1. 为FreeMarker模版准备数据
            //1.2 将文章详情数据转化为数组
            JSONArray content = JSONArray.parseArray(contentStr);

            //1.3 封装FreeMarker模板所需要的MAP数据
            Map data = new HashMap();
            data.put("content",content);

            //2. 通过FreeMaker将数据写入到模板中
            //2.1 通过Configuration获取FreeMarker模板对象
            Template template = configuration.getTemplate("article.ftl");

            //2.2 将数据写入到模板文件对应的输出流中
            StringWriter out = new StringWriter();  // 内存输出流
            template.process(data,out);


            //3. 将FreeMaker写好的文件数据上传到minIO得到URL
            //3.1 将输出流转为输入流
            ByteArrayInputStream in = new ByteArrayInputStream(out.toString().getBytes(StandardCharsets.UTF_8));

            //3.2 将文章详情输入流数据上传到minIO
            String url = fileStorageService.uploadHtmlFile("", apArticle.getId() + ".html", in);

            //4. 将URL更新到ap_article的static_url字段中
            apArticle.setStaticUrl(url);
            boolean result = apArticleService.updateById(apArticle);
            //System.out.println(result ? "生成文章详情页OK" : "生成文章详情页失败");
            log.info("[异步生成文章详情页]" + (result ? "生成文章详情页OK" : "生成文章详情页失败"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TemplateException e) {
            throw new RuntimeException(e);
        }


    }
}
